package thread.ex.chrono;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

public class CompteurGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5364186180344461376L;

	private ControleurCompteur controleur;

	private JButton bStart;
	private JButton bStop;
	private JButton bPause;
	private JButton bResume;
	private JButton bInitZero;
	private JLabel label;
	private JLabel labelH;
	private JLabel labelM;
	private JLabel labelS;
	private JLabel labelMilliS;
	private JLabel infoThread;

	private boolean conditionSTOP = false;

	public CompteurGUI() {
		super("Timer");
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);

		// Layouts
		initLayout();

		// Events
		eventBStart();
		eventBStop();
		eventBPause();
		eventBResume();
		eventBInit();

		pack();
		setVisible(false);
	}

	public void setControleur(ControleurCompteur controleur) {
		this.controleur = controleur;
	}

	private void initLayout() {
		bStart = new JButton("START");
		bStart.setBackground(Color.WHITE);
		bStop = new JButton("STOP");
		bStop.setBackground(Color.WHITE);
		bStop.setEnabled(false);
		bPause = new JButton("PAUSE");
		bPause.setBackground(Color.WHITE);
		bPause.setEnabled(false);
		bResume = new JButton("RESUME");
		bResume.setBackground(Color.WHITE);
		bResume.setEnabled(false);
		bInitZero = new JButton("ZERO");
		bInitZero.setBackground(Color.WHITE);
		bInitZero.setEnabled(false);
		label = new JLabel("All starts here...");
		labelH = new JLabel("");
		labelH.setPreferredSize(new Dimension(50, 20));
		labelM = new JLabel("");
		labelM.setPreferredSize(new Dimension(50, 20));
		labelS = new JLabel("");
		labelS.setPreferredSize(new Dimension(50, 20));
		labelMilliS = new JLabel("");
		labelMilliS.setPreferredSize(new Dimension(50, 20));
		infoThread = new JLabel("Thread : ");

		final JPanel panel = new JPanel();
		panel.add(bStart);
		panel.add(bInitZero);
		panel.add(bPause);
		panel.add(bResume);
		panel.add(bStop);
		add(panel, BorderLayout.NORTH);

		final JPanel panelLabel = new JPanel();
		panelLabel.setBackground(Color.WHITE);
		panelLabel.add(labelH);
		panelLabel.add(labelM);
		panelLabel.add(labelS);
		panelLabel.add(labelMilliS);
		add(panelLabel, BorderLayout.CENTER);
		
		final JPanel panelS = new JPanel();
		panelS.setLayout(new BorderLayout());
		panelS.add(label, BorderLayout.WEST);
		panelS.add(infoThread, BorderLayout.EAST);		
		add(panelS, BorderLayout.SOUTH);
	}

	private void eventBStart() {
		bStart.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				conditionSTOP = false;
				controleur.launch();
				updateDisplay();
				updateTime();
				String nameThread = controleur.currentThreadName();
				infoThread.setText(nameThread);
				bStart.setEnabled(false);
				bStop.setEnabled(true);
				bPause.setEnabled(true);
				bInitZero.setEnabled(true);
				bResume.setEnabled(false);
			}
		});
	}
	
	private void eventBInit() {
		bInitZero.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				controleur.initZero();
				String nameThred = controleur.currentThreadName();
				infoThread.setText(nameThred);
			}
		});
	}

	private void eventBPause() {
		bPause.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				controleur.pause();
				bPause.setEnabled(false);
				bResume.setEnabled(true);
			}
		});
	}

	private void eventBResume() {
		bResume.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				controleur.resume();
				bResume.setEnabled(false);
				bPause.setEnabled(true);
			}
		});
	}

	private void eventBStop() {
		bStop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				controleur.stop();
				conditionSTOP = true;
				bStop.setEnabled(false);
				bStart.setEnabled(true);
				bPause.setEnabled(false);
				bResume.setEnabled(false);
				bInitZero.setEnabled(false);
				infoThread.setText("");
			}
		});
	}

	private void updateDisplay() {
		new SwingWorker<String, String>() {

			protected void process(java.util.List<String> chunks) {
				label.setText(chunks.get(0));
			};

			protected void done() {
				label.setText("Complete");
			};

			@Override
			protected String doInBackground() throws Exception {
				String res = "";
				while (!conditionSTOP) {
					res = controleur.getValue();
					publish(controleur.getValue());
				}
				return res;
			}
		}.execute();
	}
	
	private void updateTime() {
		new SwingWorker<int[], int[]>() {
			protected void process(java.util.List<int[]> chunks) {
				int[] HMS = chunks.get(0);
				labelH.setText("" + HMS[0]);
				labelM.setText("" + HMS[1]);
				labelS.setText("" + HMS[2]);
				labelMilliS.setText("" + HMS[3]);
			};
			
			protected void done() {
				int[] HMS;
				try {
					HMS = get();
					labelH.setText("" + HMS[0]);
					labelM.setText("" + HMS[1]);
					labelS.setText("" + HMS[2]);
					labelMilliS.setText("" + HMS[3]);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
			
			protected int[] doInBackground() throws Exception {
				int[] HMS = {};
				while (!conditionSTOP) {
					HMS = controleur.compteurHMS();
					publish(HMS);
				}
				return HMS;
			};
		}.execute();
	}
}
